﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace System
{
    public static class DateTimeExtend
    {
        public static int LastDay(this DateTime value)
        {
            if (value.Month == 1 ||
                value.Month == 3 ||
                value.Month == 5 ||
                value.Month == 7 ||
                value.Month == 8 ||
                value.Month == 10 ||
                value.Month == 12)
            {
                return 31;
            }
            else if (value.Month == 4 ||
               value.Month == 6 ||
               value.Month == 9 ||
               value.Month == 11)
            {
                return 30;
            }
            else if (DateTime.IsLeapYear(value.Year))
            {
                return 29;
            }
            else
            {
                return 28;
            }
        }

        public static int LastOrNowDay(this DateTime value)
        {
            if (value.Year < DateTime.Now.Year||(value.Year == DateTime.Now.Year &&
                value.Month < DateTime.Now.Month))
            {
                if (value.Month == 1 ||
                    value.Month == 3 ||
                    value.Month == 5 ||
                    value.Month == 7 ||
                    value.Month == 8 ||
                    value.Month == 10 ||
                    value.Month == 12)
                {
                    return 31;
                }
                else if (value.Month == 4 ||
                   value.Month == 6 ||
                   value.Month == 9 ||
                   value.Month == 11)
                {
                    return 30;
                }
                else if (DateTime.IsLeapYear(value.Year))
                {
                    return 29;
                }
                else
                {
                    return 28;
                }
            }
            else
            {
                return DateTime.Now.Day;
            }
        }

        public static DateTime MonthLastOrNow(this DateTime value)
        {
            return new DateTime(value.Year, value.Month, value.LastOrNowDay());
        }

        public static DateTime MonthLastOrNow(int year,int month,int day)
        {
            if (year < DateTime.Now.Year || (year == DateTime.Now.Year &&
                month < DateTime.Now.Month))
            {
                if (month == 1 ||
                    month == 3 ||
                    month == 5 ||
                    month == 7 ||
                    month == 8 ||
                    month == 10 ||
                    month == 12)
                {
                    return new DateTime(year,month,31);
                }
                else if (month == 4 ||
                   month == 6 ||
                   month == 9 ||
                   month == 11)
                {
                    return  new DateTime(year,month,30);
                }
                else if (DateTime.IsLeapYear(year))
                {
                    return new DateTime(year, month, 29);
                }
                else
                {
                    return new DateTime(year, month, 28);
                }
            }
            else
            {
                if (month == 1 ||
                    month == 3 ||
                    month == 5 ||
                    month == 7 ||
                    month == 8 ||
                    month == 10 ||
                    month == 12)
                {
                    day = day > 31 ? 31 : day;
                    return new DateTime(year, month, day);
                }
                else if (month == 4 ||
                   month == 6 ||
                   month == 9 ||
                   month == 11)
                {
                    day = day > 30 ? 30 : day;
                    return new DateTime(year, month, day);
                }
                else if (DateTime.IsLeapYear(year))
                {
                    day = day > 29 ? 29 : day;
                    return new DateTime(year, month, day);
                }
                else
                {
                    day = day > 28 ? 28 : day;
                    return new DateTime(year, month, day);
                }
            }
        }
    }
}
